package com.itsm.xkitsm.liferoot.life.weather;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Execute_DB {
	public static final String city_table = "city_table",
			town_table = "town_table", province_table = "province_table";

	Context context;
	SQLiteDatabase db;

	public Execute_DB(Context context) {
		super();
		this.context = context;
	}

	public void connDB() {
		if (null == db) {
			db = new DBUtils().openDatabase(context);
		}

	}

	public void closeDB() {
		if (null != db) {
			db.close();
			db = null;
		}
	}

	public List<Object> query(String key, String table) {
		connDB();
		List<Object> data = new ArrayList<Object>();
		Cursor cursor = null;
		if (table.equalsIgnoreCase(city_table)) {
			String sql = "select * from " + table + " where town like ?";
			// System.out.println(sql);
			cursor = db.rawQuery(sql, new String[] { key });
			if (null != cursor && cursor.getCount() > 0) {
				// System.out.println("�α꣺" + cursor.getCount() + "");
				while (cursor.moveToNext()) {
					Info_city city = new Info_city(cursor.getLong(0),
							cursor.getString(1), cursor.getString(2),
							cursor.getString(3), cursor.getString(4),
							cursor.getString(5), cursor.getString(6),
							cursor.getString(7));
					data.add(city);
				}
			}
		} else if (table.equalsIgnoreCase(town_table)) {
			String sql = "select * from " + table + " where province like ?";
			// System.out.println(sql);
			cursor = db.rawQuery(sql, new String[] { key });
			if (null != cursor && cursor.getCount() > 0) {
				// System.out.println("�α꣺" + cursor.getCount() + "");
				while (cursor.moveToNext()) {
					Info_town town = new Info_town(cursor.getLong(0),
							cursor.getString(1), cursor.getString(2),
							cursor.getString(3), cursor.getString(4));
					data.add(town);
				}
			}
		} else if (table.equalsIgnoreCase(province_table)) {
			String sql = "select * from " + table + " where province like ?";
			// System.out.println(sql);
			cursor = db.rawQuery(sql, new String[] { key });
			if (null != cursor && cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					Info_province province = new Info_province(
							cursor.getLong(0), cursor.getString(1),
							cursor.getString(2));
					data.add(province);
				}
			}
		}
		if (null != cursor) {
			cursor.close();
			cursor = null;
		}

		closeDB();
		return data;

	}

	public List<Object> query_city(String key, String table) {
		connDB();
		List<Object> data = new ArrayList<Object>();
		Cursor cursor = null;
		if (table.equalsIgnoreCase(city_table)) {
			String sql = "select * from " + table + " where city like ?";
			// System.out.println(sql);
			cursor = db.rawQuery(sql, new String[] { key });
			if (null != cursor && cursor.getCount() > 0) {
				// System.out.println("�α꣺" + cursor.getCount() + "");
				while (cursor.moveToNext()) {
					Info_city city = new Info_city(cursor.getLong(0),
							cursor.getString(1), cursor.getString(2),
							cursor.getString(3), cursor.getString(4),
							cursor.getString(5), cursor.getString(6),
							cursor.getString(7));  
					data.add(city);
				}
			}
		}
		if (null != cursor) {
			cursor.close();
			cursor = null;
		}

		closeDB();
		return data;

	}

	public Info_city query_currentCity(String key, String table) {
		connDB();
		Cursor cursor = null;
		Info_city city = null;
		if (table.equalsIgnoreCase(city_table)) {
			String sql = "select * from " + table + " where city = ?";
			// System.out.println(sql);
			cursor = db.rawQuery(sql, new String[] { key });
			if (null != cursor && cursor.getCount() > 0) {
				// System.out.println("�α꣺" + cursor.getCount() + "");
				while (cursor.moveToNext()) {
					city = new Info_city(cursor.getLong(0),
							cursor.getString(1), cursor.getString(2),
							cursor.getString(3), cursor.getString(4),
							cursor.getString(5), cursor.getString(6),
							cursor.getString(7));
				}
			}
		}
		if (null != cursor) {
			cursor.close();
			cursor = null;
		}

		closeDB();
		return city;

	}
}
